package com.zhengaz.service.blog.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhengaz.common.core.constant.Constants;
import com.zhengaz.common.core.entity.R;
import com.zhengaz.service.blog.entity.BlogTypeEntity;
import com.zhengaz.service.blog.service.BlogTypeService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zhengzg
 * @create 2023-11-26 14:28
 */
@RestController
@RequestMapping(Constants.ADMIN + "/blog-type")
public class AdminBlogTypeController {

    Logger log = LoggerFactory.getLogger(AdminBlogTypeController.class);

    @Resource
    private BlogTypeService blogTypeService;

    @PostMapping("")
    @ApiOperation("添加文章类型")
    public R<Integer> save(@RequestBody BlogTypeEntity blogTypeEntity) {
        blogTypeEntity.setCreateTime(LocalDateTime.now());
        boolean save = blogTypeService.save(blogTypeEntity);
        if (save) {
            return R.ok(blogTypeEntity.getId());
        }
        return R.fail("文章类型添加失败，请稍候重试~");
    }

    @GetMapping("")
    @ApiOperation("添加文章类型")
    public R<List<BlogTypeEntity>> search(BlogTypeEntity blogTypeEntity) {
        return R.ok(blogTypeService.list(Wrappers.<BlogTypeEntity>lambdaQuery().eq(StrUtil.isNotBlank(blogTypeEntity.getType()), BlogTypeEntity::getType, blogTypeEntity.getType())));
    }

    @PutMapping("")
    @ApiOperation("修改文章类型")
    public R<Boolean> update(@RequestBody BlogTypeEntity blogTypeEntity) {
        boolean updateById = blogTypeService.updateById(blogTypeEntity);
        if (updateById) {
            return R.ok(Boolean.TRUE);
        }
        return R.fail("文章类型修改失败，请稍候重试~");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除文章类型")
    public R<Boolean> delete(@PathVariable("id") Integer id) {
        boolean removeById = blogTypeService.removeById(id);
        if (removeById) {
            return R.ok(Boolean.TRUE);
        }
        return R.fail("文章类型删除失败，请稍候重试~");
    }
}
